/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace DaveSexton.DocProject
{
	public sealed class VisualStudioInstance
	{
		#region Public Properties
		public string ProgramName
		{
			get
			{
				return programName;
			}
		}

		public string ProgramPath
		{
			get
			{
				return Path.Combine(InstallPath, programName);
			}
		}

		public string RegistryKey
		{
			get
			{
				return key;
			}
		}

		public string Version
		{
			get
			{
				return version;
			}
		}

		public string InstallPath
		{
			get
			{
				return installPath;
			}
		}
		#endregion

		#region Private / Protected
		private const string rootKey = @"SOFTWARE\Microsoft\";

		private string installPath, key;
		private readonly string programName, version;
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="VisualStudioInstance" /> class.
		/// </summary>
		internal VisualStudioInstance(string programName, string key, string version)
		{
			this.programName = programName;
			this.key = key;
			this.version = version;
		}

		/// <summary>
		/// Constructs a new instance of the <see cref="VisualStudioInstance" /> class.
		/// </summary>
		public VisualStudioInstance(string programName, string installPath, string key, string version)
			: this(programName, key, version)
		{
			this.installPath = installPath;
		}
		#endregion

		#region Methods
		internal bool Initialize()
		{
			key = System.IO.Path.Combine(rootKey, System.IO.Path.Combine(key, version));

			using (Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(key))
			{
				if (regKey != null)
				{
					installPath = regKey.GetValue("InstallDir", null) as string;

					if (!string.IsNullOrEmpty(installPath) && System.IO.Directory.Exists(installPath))
						return true;
				}
			}

			return false;
		}
		#endregion
	}
}
